package com.wjerp.tplus.server.domain.service.impl;

import com.wjerp.tplus.server.common.constant.AccountChangeType;
import com.wjerp.tplus.server.common.exception.ServiceException;
import com.wjerp.tplus.server.common.utils.WjOpenApiUtils;
import com.wjerp.tplus.server.domain.entity.CompanyAccountChangeInfo;
import com.wjerp.tplus.server.domain.entity.CompanyAccountInfo;
import com.wjerp.tplus.server.domain.mapper.CompanyAccountChangeInfoMapper;
import com.wjerp.tplus.server.domain.mapper.CompanyAccountInfoMapper;
import com.wjerp.tplus.server.domain.service.CompanyAccountInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author lishuailei
 */
@Service
public class CompanyAccountInfoServiceImpl implements CompanyAccountInfoService {

    @Resource
    private CompanyAccountInfoMapper companyAccountInfoMapper;

    @Resource
    private CompanyAccountChangeInfoMapper companyAccountChangeInfoMapper;

    @Resource
    private WjOpenApiUtils wjOpenApiUtils;

    @Override
    public CompanyAccountInfo loadCompanyAccountInfoByCompanyCode(String companyCode) {
        return companyAccountInfoMapper.selectByCompanyCode(companyCode);
    }

    @Override
    public void refreshAccount(String companyCode, String displayUserId) throws ServiceException {
        //获取当前数据库中企业账户信息
        CompanyAccountInfo companyAccountInfo = loadCompanyAccountInfoByCompanyCode(companyCode);
        //获取远端企业账户信息
        CompanyAccountInfo remoteCompanyAccountInfo = wjOpenApiUtils.refreshAccount(companyCode);

        //计算充值费用
        if(companyAccountInfo.getOcrNum() < 0){
            companyAccountInfo.setOcrNum(0);
        }

        if(companyAccountInfo.getCheckNum() < 0){
            companyAccountInfo.setCheckNum(0);
        }

        Integer ocrNum = companyAccountInfo.getOcrNum() < remoteCompanyAccountInfo.getOcrNum()
                ? remoteCompanyAccountInfo.getOcrNum() - companyAccountInfo.getOcrNum()
                : remoteCompanyAccountInfo.getOcrNum();

        Integer checkNum = companyAccountInfo.getCheckNum() < remoteCompanyAccountInfo.getCheckNum()
                ? remoteCompanyAccountInfo.getCheckNum() - companyAccountInfo.getCheckNum()
                : remoteCompanyAccountInfo.getCheckNum();

        if(companyAccountInfo.getOcrNum().equals(remoteCompanyAccountInfo.getOcrNum() )
         && companyAccountInfo.getCheckNum().equals(remoteCompanyAccountInfo.getCheckNum())){
            return;
        }
        changeCompanyAccount(companyAccountInfo,AccountChangeType.RECHARGE,ocrNum,checkNum,displayUserId);
    }

    public void changeCompanyAccount(CompanyAccountInfo companyAccountInfo, AccountChangeType accountChangeType,
                                     Integer ocrNum, Integer checkNum,String loginUserId) {
        int count = 0;
        if(accountChangeType == AccountChangeType.RECHARGE){
            count = companyAccountInfoMapper.rechargeCompanyAccount(companyAccountInfo.getId(),ocrNum,checkNum,loginUserId);
        }else{
            if(accountChangeType == AccountChangeType.ADD_CHECK_NUM ||
            accountChangeType == AccountChangeType.SUBTRACT_CHECK_NUM){
                count = companyAccountInfoMapper.updateCompanyAccount(companyAccountInfo.getId(),accountChangeType.getCode(),checkNum,loginUserId);

            }else{
                count = companyAccountInfoMapper.updateCompanyAccount(companyAccountInfo.getId(),accountChangeType.getCode(),ocrNum,loginUserId);
            }
        }

        if(count > 0){
            //增加变更历史
            CompanyAccountChangeInfo companyAccountChangeInfo = new CompanyAccountChangeInfo();
            companyAccountChangeInfo.setAccountCode(companyAccountInfo.getAccountCode());
            companyAccountChangeInfo.setUpdatedUser(loginUserId);
            companyAccountChangeInfo.setCreatedUser(loginUserId);
            companyAccountChangeInfo.setActionType(accountChangeType.getCode());
            companyAccountChangeInfo.setPreCheckNum(companyAccountInfo.getCheckNum());
            companyAccountChangeInfo.setPreOcrNum(companyAccountInfo.getOcrNum());

            companyAccountInfo = companyAccountInfoMapper.selectByPrimaryKey(companyAccountInfo.getId());

            companyAccountChangeInfo.setPostCheckNum(companyAccountInfo.getCheckNum());
            companyAccountChangeInfo.setPostOcrNum(companyAccountInfo.getOcrNum());
            companyAccountChangeInfo.setRemark(accountChangeType.getMessage());

            companyAccountChangeInfoMapper.insertSelective(companyAccountChangeInfo);
        }
    }
}
